About .RData files

Inspecting the data objects

# download RData file into your working directory
rdata <- "https://github.com/ucb-stat133/stat133-fall-2018/raw/master/data/nba2018-salary-points.RData"
download.file(url = rdata, destfile = 'nba2018-salary-points.RData')

# load data in your R session
load('nba2018-salary-points.RData')
ls()
##  [1] "age"        "experience" "player"     "points1"    "points2"   
##  [6] "points3"    "position"   "rdata"      "salary"     "scored"    
## [11] "team"

Using length() to explore the content of experience, player, and age

length(experience)
## [1] 477
length(player)
## [1] 477
length(age)
## [1] 477

Using head() to explore the content of experience, player, and age

head(experience)
## [1] "9"  "11" "6"  "R"  "9"  "5"
head(player)
## [1] "Al Horford"        "Amir Johnson"      "Avery Bradley"    
## [4] "Demetrius Jackson" "Gerald Green"      "Isaiah Thomas"
head(age)
## [1] 30 29 26 22 31 27

Using tail() to explore the content of experience, player, and age

tail(experience)
## [1] "13" "R"  "11" "2"  "R"  "15"
tail(player)
## [1] "Leandro Barbosa" "Marquese Chriss" "Ronnie Price"    "T.J. Warren"    
## [5] "Tyler Ulis"      "Tyson Chandler"
tail(age)
## [1] 34 19 33 23 21 34

Using summary() to explore the content of experience, player, and age

summary(experience)
##    Length     Class      Mode 
##       477 character character
summary(player)
##    Length     Class      Mode 
##       477 character character
summary(age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   19.00   23.00   26.00   26.39   29.00   40.00

All of the chosen objects have the same length.

There doesn’t appear to be any missing values, i.e. NA in any of the objects.

To find out the class of each of the chosen objects:

class(experience)
## [1] "character"
class(player)
## [1] "character"
class(age)
## [1] "integer"

To know if the loaded objects is a vector, use is.vector()

is.vector(experience)
## [1] TRUE

To know if a given vector is of a certain data type, use typeof() which gives the type of storage of any object:

typeof(experience)
## [1] "character"

Manipulating Vectors: Subsetting

four <- head(player, n=4)
four
## [1] "Al Horford"        "Amir Johnson"      "Avery Bradley"    
## [4] "Demetrius Jackson"
four[1]
## [1] "Al Horford"
four[0]
## character(0)
four[-1]
## [1] "Amir Johnson"      "Avery Bradley"     "Demetrius Jackson"
four[-c(1, 2, 3)]
## [1] "Demetrius Jackson"
four[5]
## [1] NA
four[c(1, 2, 2, 3, 3, 3)]
## [1] "Al Horford"    "Amir Johnson"  "Amir Johnson"  "Avery Bradley"
## [5] "Avery Bradley" "Avery Bradley"

Using seq(), rep() to extract:

All the even elements in player:

even_elem <- seq(from = 2, to = length(player), by = 2)
player[even_elem]
##   [1] "Amir Johnson"             "Demetrius Jackson"       
##   [3] "Isaiah Thomas"            "James Young"             
##   [5] "Jonas Jerebko"            "Kelly Olynyk"            
##   [7] "Terry Rozier"             "Andrew Bogut"            
##   [9] "Dahntay Jones"            "Derrick Williams"        
##  [11] "Iman Shumpert"            "James Jones"             
##  [13] "Kay Felder"               "Kyle Korver"             
##  [15] "Larry Sanders"            "Richard Jefferson"       
##  [17] "Bruno Caboclo"            "Delon Wright"            
##  [19] "DeMarre Carroll"          "Jakob Poeltl"            
##  [21] "Kyle Lowry"               "Norman Powell"           
##  [23] "Pascal Siakam"            "Serge Ibaka"             
##  [25] "Bradley Beal"             "Chris McCullough"        
##  [27] "Danuel House"             "Jason Smith"             
##  [29] "Kelly Oubre"              "Markieff Morris"         
##  [31] "Tomas Satoransky"         "DeAndre' Bembry"         
##  [33] "Dwight Howard"            "Gary Neal"               
##  [35] "Kent Bazemore"            "Lamar Patterson"         
##  [37] "Mike Dunleavy"            "Paul Millsap"            
##  [39] "Taurean Waller-Prince"    "Tim Hardaway"            
##  [41] "Gary Payton"              "Greg Monroe"             
##  [43] "Jason Terry"              "Khris Middleton"         
##  [45] "Matthew Dellavedova"      "Miles Plumlee"           
##  [47] "Rashad Vaughn"            "Steve Novak"             
##  [49] "Thon Maker"               "Aaron Brooks"            
##  [51] "C.J. Miles"               "Glenn Robinson"          
##  [53] "Joe Young"                "Lance Stephenson"        
##  [55] "Monta Ellis"              "Paul George"             
##  [57] "Rodney Stuckey"           "Anthony Morrow"          
##  [59] "Cameron Payne"            "Denzel Valentine"        
##  [61] "Isaiah Canaan"            "Jimmy Butler"            
##  [63] "Michael Carter-Williams"  "Paul Zipser"             
##  [65] "Rajon Rondo"              "Dion Waiters"            
##  [67] "Hassan Whiteside"         "Josh McRoberts"          
##  [69] "Justise Winslow"          "Okaro White"             
##  [71] "Tyler Johnson"            "Wayne Ellington"         
##  [73] "Andre Drummond"           "Beno Udrih"              
##  [75] "Darrun Hilliard"          "Ish Smith"               
##  [77] "Kentavious Caldwell-Pope" "Michael Gbinije"         
##  [79] "Reggie Jackson"           "Tobias Harris"           
##  [81] "Brian Roberts"            "Christian Wood"          
##  [83] "Frank Kaminsky"           "Johnny O'Bryant"         
##  [85] "Marco Belinelli"          "Michael Kidd-Gilchrist"  
##  [87] "Nicolas Batum"            "Treveon Graham"          
##  [89] "Chasson Randle"           "Derrick Rose"            
##  [91] "Justin Holiday"           "Kyle O'Quinn"            
##  [93] "Marshall Plumlee"         "Mindaugas Kuzminskas"    
##  [95] "Sasha Vujacic"            "Aaron Gordon"            
##  [97] "Arinze Onuaku"            "C.J. Watson"             
##  [99] "D.J. Augustin"            "Elfrid Payton"           
## [101] "Jeff Green"               "Marcus Georges-Hunt"     
## [103] "Nikola Vucevic"           "Stephen Zimmerman"       
## [105] "Alex Poythress"           "Gerald Henderson"        
## [107] "Jahlil Okafor"            "Joel Embiid"             
## [109] "Justin Harper"            "Richaun Holmes"          
## [111] "Sergio Rodriguez"         "T.J. McConnell"          
## [113] "Timothe Luwawu-Cabarrot"  "Anthony Bennett"         
## [115] "Brook Lopez"              "Greivis Vasquez"         
## [117] "Jeremy Lin"               "Justin Hamilton"         
## [119] "Luis Scola"               "Randy Foye"              
## [121] "Sean Kilpatrick"          "Trevor Booker"           
## [123] "Anderson Varejao"         "Damian Jones"            
## [125] "Draymond Green"           "James Michael McAdoo"    
## [127] "Kevin Durant"             "Klay Thompson"           
## [129] "Patrick McCaw"            "Stephen Curry"           
## [131] "Bryn Forbes"              "David Lee"               
## [133] "Dejounte Murray"          "Joel Anthony"            
## [135] "Kawhi Leonard"            "LaMarcus Aldridge"       
## [137] "Nicolas Laprovittola"     "Pau Gasol"               
## [139] "Bobby Brown"              "Clint Capela"            
## [141] "Isaiah Taylor"            "Lou Williams"            
## [143] "Nene Hilario"             "Ryan Anderson"           
## [145] "Trevor Ariza"             "Alan Anderson"           
## [147] "Blake Griffin"            "Brice Johnson"           
## [149] "DeAndre Jordan"           "J.J. Redick"             
## [151] "Luc Mbah a Moute"         "Paul Pierce"             
## [153] "Wesley Johnson"           "Boris Diaw"              
## [155] "Derrick Favors"           "Gordon Hayward"          
## [157] "Joe Ingles"               "Joel Bolomboy"           
## [159] "Rodney Hood"              "Shelvin Mack"            
## [161] "Alex Abrines"             "Domantas Sabonis"        
## [163] "Enes Kanter"              "Josh Huestis"            
## [165] "Nick Collison"            "Russell Westbrook"       
## [167] "Steven Adams"             "Victor Oladipo"          
## [169] "Brandan Wright"           "Deyonta Davis"           
## [171] "JaMychal Green"           "Marc Gasol"              
## [173] "Toney Douglas"            "Troy Daniels"            
## [175] "Wade Baldwin"             "Zach Randolph"           
## [177] "Allen Crabbe"             "Damian Lillard"          
## [179] "Evan Turner"              "Jusuf Nurkic"            
## [181] "Meyers Leonard"           "Pat Connaughton"         
## [183] "Alonzo Gee"               "Darrell Arthur"          
## [185] "Gary Harris"              "Jameer Nelson"           
## [187] "Juan Hernangomez"         "Malik Beasley"           
## [189] "Mike Miller"              "Roy Hibbert"             
## [191] "Wilson Chandler"          "Anthony Davis"           
## [193] "Dante Cunningham"         "Donatas Motiejunas"      
## [195] "Jarrett Jack"             "Jrue Holiday"            
## [197] "Omri Casspi"              "Reggie Williams"         
## [199] "Tim Frazier"              "Ben Bentil"              
## [201] "Devin Harris"             "Dorian Finney-Smith"     
## [203] "Harrison Barnes"          "Jarrod Uthoff"           
## [205] "Manny Harris"             "Nicolas Brussino"        
## [207] "Salah Mejri"              "Wesley Matthews"         
## [209] "Arron Afflalo"            "Buddy Hield"             
## [211] "Garrett Temple"           "Jordan Farmar"           
## [213] "Langston Galloway"        "Rudy Gay"                
## [215] "Ty Lawson"                "Willie Cauley-Stein"     
## [217] "Andrew Wiggins"           "Cole Aldrich"            
## [219] "John Lucas III"           "Karl-Anthony Towns"      
## [221] "Nemanja Bjelica"          "Shabazz Muhammad"        
## [223] "Zach LaVine"              "Corey Brewer"            
## [225] "David Nwaba"              "Jordan Clarkson"         
## [227] "Luol Deng"                "Nick Young"              
## [229] "Thomas Robinson"          "Tyler Ennis"             
## [231] "Alex Len"                 "Derrick Jones"           
## [233] "Dragan Bender"            "Eric Bledsoe"            
## [235] "Jarell Eddie"             "Leandro Barbosa"         
## [237] "Ronnie Price"             "Tyler Ulis"

All the odd elements in salary:

odd_elem <- seq(from = 1, to = length(salary), by = 2)
salary[odd_elem]
##   [1] 26540100  8269663  1410598  6286408  4743000  1223653  3578880
##   [8]  8000000  7806971   259626     5145 12800000   874636 21165675
##  [15] 17638063 30963450 15330435  7330000 26540100   543471 14382022
##  [22]  1921320  5300000  6050000  3730653  1200000   543471 15944154
##  [29] 16957900 12000000  5893981  3386598  2708582  8400000   392478
##  [36]  4000000  2500000  1015696   418228  3850000    51449  2995421
##  [43]  5374320 12517606   925000  1403611 10500000  6348759   236457
##  [50]  2368327 10230179   650000  8800000  1800000  4000000  2463840
##  [57]  1052342 14153652  1453680   874636 23200000  1643040  1709720
##  [64]  5782450   425000 13219250 15890000  4000000   874636  1227000
##  [71]   543471  4000000  1015696  6500000  7000000  1704120 10991957
##  [78]  4625000  2255644  2969880   375579   231521  5318313  6511628
##  [85] 12000000 12250000   138938  6000000 24559380 11242000 17000000
##  [92]  4317720  6191000   543471   543471  1375000    51449 17000000
##  [99]  1209680   980431 17000000  6540000  3909840    31969 10000000
## [106]  2318280   442126  9424084  1514160  2993040  1015696    96969
## [113]  8550000  6088993   194494  1562280  1074145   980431  3333333
## [120]  1914544  1395600   726672   202300 11131368  1551659  1015696
## [127]  1403611  1182840   383351  5782450  2898000 10000000   543471
## [134]  2898000   874636  1192080 14000000  3578948 14445313   543471
## [141] 12385364 26540100  1045000  6000000  1720560   181969 11000000
## [148]  1551659 22868827   543471 13253012  1403611  1551659 10154495
## [155]  3940320  8000000  1015696 11000000   937800  2121288  2340600
## [162]  2183072  2483040   980431  4837500   247991   543471  8950000
## [169]   945000 22116750  2898000  1286160 26540100  5505618  4264057
## [176]    83119  7680965  3219579  6666667   600000  8988764  2751360
## [183]  1350120 15050000  3241800  3210840   150000 12078652  2328530
## [190]  1358500  3533333  4600000   543471 16957900  8081363   234915
## [197]  9904494    79922 11241218   650000  1015696 25000000  8375000
## [204]  4096950   680937  4384490   105498  2898000  8000000  4008882
## [211]  5229454  2202240  8046500  1439880  1188840 10661286  2022240
## [218]  3500000  2348783  3911380  3872520 13550000  1339680  5281680
## [225]  5332800  1034956  3267120  1551659  6191000 16000000   874636
## [232] 12606250  2223600    23069 10470000   469841  2941440  2128920
## [239] 12415000

All multiples of 5 (e.g. 5, 10, 15, etc) of team:

by_5 <- seq(from = 5, to = length(team), by = 5)
team[by_5]
##  [1] BOS BOS BOS CLE CLE CLE TOR TOR TOR WAS WAS WAS ATL ATL ATL ATL MIL
## [18] MIL MIL IND IND IND IND CHI CHI CHI MIA MIA MIA DET DET DET CHO CHO
## [35] CHO NYK NYK NYK ORL ORL ORL PHI PHI PHI PHI BRK BRK BRK BRK GSW GSW
## [52] GSW SAS SAS SAS HOU HOU HOU LAC LAC LAC UTA UTA UTA OKC OKC OKC MEM
## [69] MEM MEM POR POR POR DEN DEN DEN NOP NOP NOP DAL DAL DAL DAL SAC SAC
## [86] SAC MIN MIN MIN LAL LAL LAL PHO PHO PHO
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS

Elements in positions 10, 20, 30, etc of scored:

by_10 <- seq(from = 10, to = length(scored), by = 10)
scored[by_10]
##  [1]  299  156    4  165 1779 1063  801 1143  577  322  630  291  538   98
## [15]  127 1321  780 1154  124 1046   64  559 1539  357  776 1999 1888  818
## [29]  936 1173  476  430   33   58  106  304  851    4    6    0    8  378
## [43]  681 2061  120  170   24
even_elem_team <- seq(from = length(team), to = 2, by = -2)
team[even_elem_team]
##   [1] PHO PHO PHO PHO PHO PHO PHO PHO PHO LAL LAL LAL LAL LAL LAL LAL MIN
##  [18] MIN MIN MIN MIN MIN MIN SAC SAC SAC SAC SAC SAC SAC SAC DAL DAL DAL
##  [35] DAL DAL DAL DAL DAL DAL NOP NOP NOP NOP NOP NOP NOP NOP DEN DEN DEN
##  [52] DEN DEN DEN DEN DEN POR POR POR POR POR POR POR MEM MEM MEM MEM MEM
##  [69] MEM MEM MEM OKC OKC OKC OKC OKC OKC OKC UTA UTA UTA UTA UTA UTA UTA
##  [86] UTA LAC LAC LAC LAC LAC LAC LAC HOU HOU HOU HOU HOU HOU HOU SAS SAS
## [103] SAS SAS SAS SAS SAS SAS GSW GSW GSW GSW GSW GSW GSW GSW BRK BRK BRK
## [120] BRK BRK BRK BRK BRK BRK BRK PHI PHI PHI PHI PHI PHI PHI PHI ORL ORL
## [137] ORL ORL ORL ORL ORL ORL ORL NYK NYK NYK NYK NYK NYK NYK NYK CHO CHO
## [154] CHO CHO CHO CHO CHO CHO DET DET DET DET DET DET DET MIA MIA MIA MIA
## [171] MIA MIA MIA CHI CHI CHI CHI CHI CHI CHI CHI IND IND IND IND IND IND
## [188] IND IND MIL MIL MIL MIL MIL MIL MIL MIL MIL MIL ATL ATL ATL ATL ATL
## [205] ATL ATL ATL WAS WAS WAS WAS WAS WAS WAS WAS TOR TOR TOR TOR TOR TOR
## [222] TOR CLE CLE CLE CLE CLE CLE CLE CLE CLE BOS BOS BOS BOS BOS BOS BOS
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS

Logical Subsetting

Players in position of Center, of Warriors(GSW)

player[which(position == "C")]
##  [1] "Al Horford"           "Kelly Olynyk"         "Tyler Zeller"        
##  [4] "Andrew Bogut"         "Channing Frye"        "Edy Tavares"         
##  [7] "Larry Sanders"        "Tristan Thompson"     "Jakob Poeltl"        
## [10] "Jonas Valanciunas"    "Lucas Nogueira"       "Daniel Ochefu"       
## [13] "Ian Mahinmi"          "Jason Smith"          "Marcin Gortat"       
## [16] "Dwight Howard"        "Mike Muscala"         "Greg Monroe"         
## [19] "John Henson"          "Miles Plumlee"        "Thon Maker"          
## [22] "Al Jefferson"         "Myles Turner"         "Cristiano Felicio"   
## [25] "Joffrey Lauvergne"    "Robin Lopez"          "Hassan Whiteside"    
## [28] "Udonis Haslem"        "Willie Reed"          "Andre Drummond"      
## [31] "Aron Baynes"          "Boban Marjanovic"     "Cody Zeller"         
## [34] "Frank Kaminsky"       "Mike Tobey"           "Joakim Noah"         
## [37] "Kyle O'Quinn"         "Marshall Plumlee"     "Willy Hernangomez"   
## [40] "Arinze Onuaku"        "Bismack Biyombo"      "Nikola Vucevic"      
## [43] "Stephen Zimmerman"    "Jahlil Okafor"        "Joel Embiid"         
## [46] "Richaun Holmes"       "Shawn Long"           "Tiago Splitter"      
## [49] "Brook Lopez"          "Justin Hamilton"      "Anderson Varejao"    
## [52] "Damian Jones"         "David West"           "JaVale McGee"        
## [55] "Kevon Looney"         "Zaza Pachulia"        "Dewayne Dedmon"      
## [58] "Joel Anthony"         "Pau Gasol"            "Chinanu Onuaku"      
## [61] "Clint Capela"         "Montrezl Harrell"     "Nene Hilario"        
## [64] "DeAndre Jordan"       "Diamond Stone"        "Marreese Speights"   
## [67] "Jeff Withey"          "Rudy Gobert"          "Enes Kanter"         
## [70] "Steven Adams"         "Deyonta Davis"        "Marc Gasol"          
## [73] "Jusuf Nurkic"         "Jarnell Stokes"       "Mason Plumlee"       
## [76] "Nikola Jokic"         "Roy Hibbert"          "Alexis Ajinca"       
## [79] "Anthony Davis"        "DeMarcus Cousins"     "Omer Asik"           
## [82] "A.J. Hammons"         "Dwight Powell"        "Nerlens Noel"        
## [85] "Salah Mejri"          "Georgios Papagiannis" "Kosta Koufos"        
## [88] "Willie Cauley-Stein"  "Cole Aldrich"         "Jordan Hill"         
## [91] "Karl-Anthony Towns"   "Ivica Zubac"          "Tarik Black"         
## [94] "Timofey Mozgov"       "Alan Williams"        "Alex Len"            
## [97] "Tyson Chandler"

Players of either GSW (warriors) or LAL (lakers)

player[which(team == "GSW" | team == "LAL")]
##  [1] "Anderson Varejao"     "Andre Iguodala"       "Damian Jones"        
##  [4] "David West"           "Draymond Green"       "Ian Clark"           
##  [7] "James Michael McAdoo" "JaVale McGee"         "Kevin Durant"        
## [10] "Kevon Looney"         "Klay Thompson"        "Matt Barnes"         
## [13] "Patrick McCaw"        "Shaun Livingston"     "Stephen Curry"       
## [16] "Zaza Pachulia"        "Brandon Ingram"       "Corey Brewer"        
## [19] "D'Angelo Russell"     "David Nwaba"          "Ivica Zubac"         
## [22] "Jordan Clarkson"      "Julius Randle"        "Luol Deng"           
## [25] "Metta World Peace"    "Nick Young"           "Tarik Black"         
## [28] "Thomas Robinson"      "Timofey Mozgov"       "Tyler Ennis"

Players in position Shooting Guard or Point Guards, of Lakers

player[which((position == "SG" | position == "PG") & team == "LAL")]
## [1] "D'Angelo Russell" "David Nwaba"      "Jordan Clarkson" 
## [4] "Nick Young"       "Tyler Ennis"

Subset of Small Forwards of GSW and LAL

player[which((team == "GSW" | team == "LAL") & (position == "SF"))]
## [1] "Andre Iguodala"    "Matt Barnes"       "Brandon Ingram"   
## [4] "Corey Brewer"      "Luol Deng"         "Metta World Peace"

Name of the player with the largest salary

player[which.max(salary)] #or player[salary == max(salary)]
## [1] "LeBron James"

Name of the palyer with the smallest salary

player[which.min(salary)] #or player[salary == min(salary)]
## [1] "Edy Tavares"

Name of the player with the largest number of scored points

player[which.max(scored)]
## [1] "Russell Westbrook"

Salary of the player with the largest number of points

salary[player == player[which.max(scored)]]
## [1] 26540100

Largest salary of all the Centers

max(salary[which(position == "C")])
## [1] 26540100

Team of the player with the largest number of scored points

team[which(player == player[which.max(scored)])]
## [1] OKC
## 30 Levels: ATL BOS BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL ... WAS

Name of the player with the largest number of 3-pointers

player[which.max(points3)]
## [1] "Stephen Curry"

Subsetting with Character Vectors

Some plotting

#install.packages(c("ggplot2", "plotly"))
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(x = scored, y = salary, type = "scatter", mode = "markers")

An issue with this generated plot is that it doesn’t have any labels.

log_scored <- log(scored)
log_salary <- log(salary)
plot(log_scored, log_salary)
text(log_scored, log_salary, labels = abbreviate(player))

Create a scatterplot of points and salary for the Warriors (GSW), displaying the names of the players. Generate two scatter plots, one with raw values(original scale, and another plot with log-transformations).

plot_ly(x = scored[which(team == "GSW")], y = salary[which(team == "GSW")], type = "scatter", mode = "markers")
gsw_log_scored <- log(scored[which(team == "GSW")])
gsw_log_salary <- log(salary[which(team == "GSW")])
plot(gsw_log_scored, gsw_log_salary)
text(gsw_log_scored, gsw_log_salary, labels = player[which(team == "GSW")])

Recycling

Create a new vector salary_millions with the converted values in millions of dollars

salary_millions <- salary / 1000000
salary_millions
##   [1] 26.540100 12.000000  8.269663  1.450000  1.410598  6.587132  6.286408
##   [8]  1.825200  4.743000  5.000000  1.223653  3.094014  3.578880  1.906440
##  [15]  8.000000  0.242224  7.806971  0.024022  0.259626  0.402043  0.005145
##  [22]  9.700000 12.800000  1.551659  0.874636  0.543471 21.165675  5.239437
##  [29] 17.638063  0.207722 30.963450  2.500000 15.330435  1.589640  7.330000
##  [36]  1.577280 26.540100 14.200000  0.543471  2.703960 14.382022 12.000000
##  [43]  1.921320  0.874636  5.300000  1.196040  6.050000 12.250000  3.730653
##  [50] 22.116750  1.200000  1.191480  0.543471  0.543471 15.944154  5.000000
##  [57] 16.957900  2.006640 12.000000  7.400000  5.893981  2.870813  3.386598
##  [64]  1.499760  2.708582 23.180275  8.400000  0.072193  0.392478 15.730338
##  [71]  4.000000  0.128623  2.500000  4.837500  1.015696 20.072033  0.418228
##  [78]  2.318280  3.850000  2.281605  0.051449  0.035166  2.995421 17.100000
##  [85]  5.374320  1.551659 12.517606 15.200000  0.925000  9.607500  1.403611
##  [92] 12.500000 10.500000  1.811040  6.348759  1.551659  0.236457  2.568600
##  [99]  2.368327  2.700000 10.230179  4.583450  0.650000  1.050500  8.800000
## [106]  1.052342  1.800000  4.000000  4.000000 10.770000  2.463840 18.314532
## [113]  1.052342  7.000000 14.153652  3.488000  1.453680  2.112480  0.874636
## [120]  2.092200 23.200000  1.015696  1.643040 17.552209  1.709720  3.183526
## [127]  5.782450  0.750000  0.425000 14.000000 13.219250  2.898000 15.890000
## [134] 22.116750  4.000000  5.782450  0.874636  2.593440  1.227000  0.374933
## [141]  0.543471  5.628000  4.000000  6.000000  1.015696 22.116750  6.500000
## [148]  1.551659  7.000000  0.874636  1.704120  6.000000 10.991957  3.678319
## [155]  4.625000  0.650000  2.255644 14.956522  2.969880 17.200000  0.375579
## [162]  1.050961  0.231521  0.874636  5.318313  2.730000  6.511628  0.276827
## [169] 12.000000  6.333333 12.250000 13.000000  0.138938 20.869566  6.000000
## [176]  0.543471 24.559380  0.243860 11.242000 21.323250 17.000000  1.015696
## [183]  4.317720  3.900000  6.191000  0.543471  0.543471  2.898000  0.543471
## [190]  1.410598  1.375000  4.351320  0.051449  0.440078 17.000000  5.000000
## [197]  1.209680  7.250000  0.980431  2.613600 17.000000 15.000000  6.540000
## [204]  0.031969  3.909840 11.750000  0.031969  0.950000 10.000000  0.038903
## [211]  2.318280  9.000000  0.442126  4.788840  9.424084  4.826160  1.514160
## [218]  0.057672  2.993040  1.025831  1.015696  8.000000  0.096969  0.874636
## [225]  8.550000  1.326960  6.088993  1.015696  0.194494 21.165675  1.562280
## [232]  4.347826  1.074145 11.483254  0.980431  3.000000  3.333333  5.000000
## [239]  1.914544  2.500000  1.395600  0.980431  0.726672  9.250000  0.202300
## [246]  1.551659 11.131368  1.171560  1.551659 15.330435  1.015696  0.980431
## [253]  1.403611 26.540100  1.182840 16.663575  0.383351  0.543471  5.782450
## [260] 12.112359  2.898000  0.543471 10.000000  1.551659  0.543471  1.180080
## [267]  2.898000  0.663810  0.874636 17.638063  1.192080 20.575005 14.000000
## [274]  0.210995  3.578948 15.500000 14.445313  0.934293  0.543471  1.296240
## [281] 12.385364  0.305000 26.540100  7.000000  1.045000  2.898000  6.000000
## [288] 18.735364  1.720560  7.806971  0.181969  1.315448 11.000000 20.140838
## [295]  1.551659  1.273920 22.868827 21.165675  0.543471  7.377500 13.253012
## [302]  2.203000  1.403611  3.500000  1.551659  5.628000 10.154495  7.000000
## [309]  3.940320 11.050000  8.000000 16.073140  1.015696  2.250000 11.000000
## [316]  0.600000  0.937800  1.406520  2.121288  2.433334  2.340600  5.994764
## [323]  2.183072  2.440200  2.483040 17.145838  0.980431  1.191480  4.837500
## [330]  3.750000  0.247991 26.540100  0.543471  3.140517  8.950000  6.552960
## [337]  0.945000  5.700000 22.116750  1.369229  2.898000  0.980431  1.286160
## [344] 21.165675 26.540100  0.603558  5.505618  3.332940  4.264057  1.793760
## [351]  0.083119 10.361445  7.680965 18.500000  3.219579 24.328425  6.666667
## [358] 16.393443  0.600000  1.921320  8.988764  9.213484  2.751360  0.874636
## [365]  1.350120  0.495227 15.050000  8.070175  3.241800  1.655880  3.210840
## [372]  4.540525  0.150000  1.987440 12.078652  1.627320  2.328530  3.500000
## [379]  1.358500  5.000000  3.533333 11.200000  4.600000 22.116750  0.543471
## [386]  2.978250 16.957900  0.576724  8.081363  0.091274  0.234915 11.286518
## [393]  9.904494  3.000000  0.079922  0.252676 11.241218  2.090000  0.650000
## [400]  0.031969  1.015696  4.228000 25.000000  0.543471  8.375000 22.116750
## [407]  4.096950  0.063938  0.680937  0.119494  4.384490  0.543471  0.105498
## [414]  0.874636  2.898000 17.100000  8.000000 12.500000  4.008882  3.517200
## [421]  5.229454  8.000000  2.202240  0.082979  8.046500  5.200000  1.439880
## [428] 13.333333  1.188840  1.315448 10.661286  3.551160  2.022240  6.006600
## [435]  3.500000  7.643979  2.348783  0.635753  3.911380  5.960160  3.872520
## [442]  3.800000 13.550000  3.046299  1.339680  2.240880  5.281680  7.600000
## [449]  5.332800  0.137466  1.034956 12.500000  3.267120 18.000000  1.551659
## [456]  5.443918  6.191000  1.050961 16.000000  1.733880  0.874636  4.823621
## [463] 12.606250  0.543471  2.223600  4.276320  0.023069 14.000000 10.470000
## [470]  0.102898  0.469841  4.000000  2.941440  0.465143  2.128920  0.918369
## [477] 12.415000

Take the values in points, points2, and points3 and create a new vector scored_points that gives you the same values as scored

scored_points <- points1 + (2 * points2) + (3 * points3)
scored_points
##   [1]  952  520  894   10  262 2199  999   68  515  299   38  678  835  410
##  [15]  178    0  676    9  179  156    6  567  351  132  161  166 1142  373
##  [29] 1816    4 1954  448  630   14  740  150 2020  638  107  165  959 1344
##  [43]  253  636  139  229  445  327  330 1779   81    1   24    0  173  420
##  [57] 1805  501  883 1063 1075  154  285  101 1414 1002  270    4   61  801
##  [71]  257    9  391  169  435 1246   25  335  444 1143    0   20 1832  951
##  [85] 1025  307  392  426  767  577  528   82  451  142   83    5    0  226
##  [99]  683  322  535  815   21  419 1254   68  232   43  177  630 1173 1775
## [113]   59  281  814   41  437   54  316  291 1096  181  370 1816   89  297
## [127]  744  240    0  538  839  729 1483 1309  975  107  539  196  324   98
## [141]  497 1002   31  648  374 1105  365  227  191  127   60  758  767 1047
## [155] 1105    4  141  752  339 1321    1  142   50   35  639  874  603   18
## [169] 1830  780  849  743    2 1164  312   57 1659   95  835 1154  232  629
## [183] 1196  496  275   40   98  425  215  124  587 1019    9    4  483  281
## [197]   21  616   82 1046 1167  638  327   14  317 1096    0   23  299   64
## [211] 1040  662  171  590   33  627  203   12  756  559  864  530  148  556
## [225]   39  445   30  115   95 1539  468    7  543  523  428  442  126  184
## [239]  209  357  675  919  432  709   54   18  574   19  316  776  527  147
## [253]  472 1555  135 1742  114  282  389 1999  426   94  497  576  303  130
## [267]  387   25  483 1888  246 1243  517   59  759  792  638   62   14  818
## [281] 1217    3 2356  343  527  609  639  979  504  936   58   86  889 1316
## [295]  292    4 1104 1029   10 1173 1008  484  711   81  538  186  283  338
## [309]  412  476  829 1601  146  581  715   22  100  748 1137  430  440  406
## [323]  522  479  145 1033  421   14   88   33   43 2558  183  905  207 1067
## [337]  425  189  210   58  429  689  165 1446 1415  117  643  551  586  106
## [351]   55 1028  532  845 1837 2024  200  586   78  304  773  401  327   98
## [365]  218   11 1145  262  603  851  811  687    3  305  587   83  245   28
## [379] 1221    4  820 1117  207 2099   87  435  414  150  700    6  267 1029
## [393]   85   12   52   27  563  464   48    0    8  437  769  350  516 1518
## [407]  381   40  106    8  188  150   35  213  898  986  461  515  495  378
## [421]  900  506  124   12  470  114   79  562  289  681  163  611   63 1933
## [435]  197  105  816    2   12 2061  293  403  836  772  209  889  740  129
## [449]  984  120  284 1205  975  425   57  791  383  241  401  170  346  613
## [463]  595  168 1726  146    3 1390  434   24    7  419  753   14  951  444
## [477]  397

Factors

is.factor(team)
## [1] TRUE
position_fac <- factor(position)
position_fac[1:5]
## [1] C  PF SG PG SF
## Levels: C PF PG SF SG
table(position_fac)
## position_fac
##   C  PF  PG  SF  SG 
##  97  98  96  84 102

Practice manipulating position_fac to get:

Positions of Warriors

position_fac[which(team == "GSW")]
##  [1] C  SF C  C  PF SG PF C  PF C  SG SF SG PG PG C 
## Levels: C PF PG SF SG

Positions of players with salaries > 15 millions

position_fac[which(salary_millions > 15)]
##  [1] C  PF PG SF C  SG SG C  PG C  SF PF C  SF SF SG SF PG C  C  PF SG SF
## [24] PG C  C  SG C  PF PF SG SF PF C  PG PF PF PG C  SF C  PG SF C  PG SG
## [47] PG SF SF C  C  PF PF SG SF C 
## Levels: C PF PG SF SG

Frequencies (counts) of positions with salaries > 15 millions

table(position_fac[which(salary_millions > 15)])
## 
##  C PF PG SF SG 
## 17 10  9 12  8

Relative frequencies (proportions) of Shooting Guards (‘SG’) in each team

team_fac <- factor(team)
table(team_fac[position == "SG"]) / table(team_fac)
## 
##       ATL       BOS       BRK       CHI       CHO       CLE       DAL 
## 0.1764706 0.2000000 0.2105263 0.3125000 0.3125000 0.2222222 0.2222222 
##       DEN       DET       GSW       HOU       IND       LAC       LAL 
## 0.2352941 0.2000000 0.1875000 0.2142857 0.1250000 0.2000000 0.2142857 
##       MEM       MIA       MIL       MIN       NOP       NYK       OKC 
## 0.1875000 0.2857143 0.2105263 0.1428571 0.1250000 0.2666667 0.1333333 
##       ORL       PHI       PHO       POR       SAC       SAS       TOR 
## 0.2777778 0.1764706 0.2941176 0.2307692 0.3125000 0.2500000 0.1333333 
##       UTA       WAS 
## 0.1333333 0.2000000

More plots

plot(scored, salary, col = position_fac)

plot(scored, salary, col = position_fac, pch = 3)

plot(scored, salary, col = position_fac, pch = 1)

plot(scored, salary, col = position_fac, pch = 3, cex = 3)

plot(scored, salary, col = position_fac, pch = 3, cex = 10)

plot(scored, salary, col = position_fac, pch = 3, cex = 0.4)

plot(scored, salary, col = position_fac, pch = 3, cex = 0.1)

plot(scored, salary, col = position_fac, pch = 3, cex = 0.4, xlab = "scores", ylab = "salary in millions")